//
//  RectangleLayer.swift
//  AnimationGuard
//
//  Created by onemade on 11/4/15.
//  Copyright © 2015 CNPC. All rights reserved.
//

import UIKit

class RectangleLayer: CAShapeLayer {
    override init() {
        super.init()
        fillColor = Colors.clear.CGColor
        lineWidth = 5.0
        path = rectanglePathFull.CGPath
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented.")
    }
    
    var rectanglePathFull : UIBezierPath {
        let rectanglePath = UIBezierPath()
        rectanglePath.moveToPoint(CGPoint(x: 0.0, y: 100.0))
        rectanglePath.addLineToPoint(CGPoint(x: 0.0, y: CGFloat(-lineWidth)))
        rectanglePath.addLineToPoint(CGPoint(x: 100.0, y: CGFloat(-lineWidth)))
        rectanglePath.addLineToPoint(CGPoint(x: 100.0, y: 100.0))
        rectanglePath.addLineToPoint(CGPoint(x: CGFloat(-lineWidth/2), y: 100.0))
        rectanglePath.closePath()
        return rectanglePath
    }
    
    func animateStrokeWithColor(coler:UIColor){
        strokeColor = coler.CGColor
        
        let strokeAnimation: CABasicAnimation = CABasicAnimation(keyPath: "strokeEnd")
        strokeAnimation.fromValue = 0.0
        strokeAnimation.toValue = 1.0
        strokeAnimation.duration = 0.45
        addAnimation(strokeAnimation, forKey: nil)
    }
}
